package com.iims.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iims.common.exception.CustomException;
import com.iims.common.utils.SeqNumUtils;
import com.iims.finance.domain.CmisCwCollection;
import com.iims.finance.enums.ReviewStatus;
import com.iims.finance.mapper.CmisCwCollectionMapper;
import com.iims.finance.service.ICmisCwCollectionService;
import com.iims.finance.utils.FinanceBusinessKeyUtils;
import com.iims.finance.vo.resp.CmisCwCollectionDetailVo;
import com.iims.flowable.service.IFlowInstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/7/9 22:31
 */
@Service
public class CmisCwCollectionServiceImpl extends ServiceImpl<CmisCwCollectionMapper, CmisCwCollection> implements ICmisCwCollectionService {

    @Autowired
    private IFlowInstanceService instanceService;

    @Override
    public List<CmisCwCollectionDetailVo> getCwCollectionList(CmisCwCollection collection) {
        List list =  baseMapper.selectCmisCwCollectionList(collection);
        return SeqNumUtils.initSeqNum(collection, list);
    }

    @Override
    public List<CmisCwCollectionDetailVo> getContractCwCollectionList(Long contractId) {
        return baseMapper.selectContractCmisCwCollectionList(contractId);
    }

    @Override
    public CmisCwCollectionDetailVo getCwCollectionDetailById(Long collectId) {
        return baseMapper.selectCmisCwCollectionDetail(collectId);
    }

    @Override
    public void insertCwCollection(CmisCwCollection collection) {
        baseMapper.insert(collection);
    }

    @Override
    public void updateCwCollection(CmisCwCollection collection) {
        LambdaUpdateWrapper<CmisCwCollection> updateWrapper = new LambdaUpdateWrapper<CmisCwCollection>()
                .eq(CmisCwCollection::getCollectId, collection.getCollectId())
                .set(CmisCwCollection::getContractId, collection.getContractId())
                .set(CmisCwCollection::getAccountName, collection.getAccountName())
                .set(CmisCwCollection::getAccountNum, collection.getAccountNum())
                .set(CmisCwCollection::getCollectDate, collection.getCollectDate())
                .set(CmisCwCollection::getCollectUser, collection.getCollectUser())
                .set(CmisCwCollection::getNeedMoney, collection.getNeedMoney())
                .set(CmisCwCollection::getReallyCollectMoney, collection.getReallyCollectMoney())
                .set(CmisCwCollection::getCollectType, collection.getCollectType())
                .set(CmisCwCollection::getRemark, collection.getRemark());
        baseMapper.update(null, updateWrapper);
    }

    @Override
    public void deleteById(Long collectId) {
        checkCmisCwCollectionUse(collectId);
        baseMapper.deleteById(collectId);
    }

    @Override
    public void checkCmisCwCollectionUse(Long collectId) {
        CmisCwCollection collection = baseMapper.selectOne(new LambdaQueryWrapper<CmisCwCollection>()
                .select(CmisCwCollection::getCollectId, CmisCwCollection::getReviewStatus).eq(CmisCwCollection::getCollectId, collectId));

        if (ReviewStatus.HasReviewed.getValue() == collection.getReviewStatus().intValue()) {
            throw new CustomException("已审核的收款记录禁止删除!");
        }
        String businessKey = FinanceBusinessKeyUtils.getCollectionBusinessKey(collection);
        if (instanceService.checkBusinessInFlow(businessKey)) {
            throw new CustomException("收款记录已在流程中运转,无法删除!");
        }
    }
}
